package com.ce.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


public class DownLoadThread extends Thread{

	
	private String urlPath;
	
	private String fileName;
	
	private String parentMkdirs;
	
	private Handler handler;
	
	private int position;
	
	private int what;
	
	public DownLoadThread(Handler handler, int position
						 ,String urlPath, String fileName
						 ,String parentMkdirs, int what) {
		this.handler = handler;
		this.position = position;
		this.urlPath = urlPath;
		this.fileName = fileName;
		this.parentMkdirs = parentMkdirs;
		this.what = what;
	}
	
	@Override
	public void run() {
		boolean result = writeImgToSD();
		if(result){
			Message msg = handler.obtainMessage();
			msg.what = this.what;
			msg.arg1 = this.position;
			msg.obj = (this.parentMkdirs == null 
					  || "".equals(this.parentMkdirs.trim())) 
					  ? fileName 
					  : this.parentMkdirs + "/" + fileName;
			msg.sendToTarget();
		}
	}
	
	public boolean writeImgToSD()  {
			String status = Environment.getExternalStorageState();
			if(!status.equals(Environment.MEDIA_MOUNTED)) {
				Log.d(DishDefine.TAG, "check SdCard fail...");
				return false;
			}
		
		if(fileName == null || "".equals(fileName.trim())) {
			Log.d(DishDefine.TAG, "the paramer 'fileName' is null.");
			return false;
		}
		String filePath = (this.parentMkdirs == null 
						  || "".equals(this.parentMkdirs)) 
						  ? fileName 
						  : this.parentMkdirs + "/" + fileName;
		File file = new File(filePath);
		if(FileUtils.isFileExists(file))
			return true;
		boolean createMkdirs = false ;
		try{
			createMkdirs = FileUtils.createMkdirs(parentMkdirs);
		}catch(Exception e) {
			e.printStackTrace();
		}
		if(!createMkdirs) {
			Log.d(DishDefine.TAG, "create Mkdirs is error.");
			return false;
		}
		FileOutputStream outputStream = null; 
		InputStream inputStream = null;
		HttpURLConnection connection = null;
		try{		
			outputStream = new FileOutputStream(DishDefine.PARENT_PATH + "/" + file);
			URL url = new URL(DishDefine.SERVER_IMG_URL + ((urlPath == null || "".equals(urlPath.trim())) ? fileName : (urlPath + fileName)));
			connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(3000);
			connection.setReadTimeout(3000);
			connection.connect();
			inputStream = connection.getInputStream();
			byte[] buf = new byte[2 * 1024];
			int length = 0 ;
			while((length = inputStream.read(buf)) != -1 ) {
				outputStream.write(buf, 0, length);
				outputStream.flush();
			}
			return true;
		}catch(Exception e) {
			e.printStackTrace();
			Log.d(DishDefine.TAG, e.getMessage());
		}finally{
			try{
				if(outputStream != null) {
					outputStream.close();
					outputStream = null;
				}
				if(inputStream != null) {
					inputStream.close();
					inputStream = null;
				}
			}catch(Exception e) {
				e.printStackTrace();
				Log.d(DishDefine.TAG, e.getMessage());
			}
			connection.disconnect();
		}
		return false;
	}
}
